package me.zhengjie.modules.system.rest;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.system.service.ProcessTaskService;
import org.flowable.task.api.Task;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequiredArgsConstructor
@Api(tags = "个人流程任务")
@RequestMapping("/api/processTask")
public class ProcessTaskController {

    private final ProcessTaskService processTaskService;

    @GetMapping(value = "/myInitiation")
    @ApiOperation("我的发起")
    @Log("我的发起")
    @AnonymousAccess
    public ResponseEntity<Object> myInitiation(Long userId) throws Exception {
        return new ResponseEntity<>(processTaskService.getMyInitiation(userId), HttpStatus.OK);
    }

    @GetMapping(value = "/myTodo")
    @ApiOperation("我的待办")
    @Log("我的待办")
    @AnonymousAccess
    public ResponseEntity<Object> myTodo(Long userId){
        return new ResponseEntity<>(processTaskService.getMyTodoList(userId),HttpStatus.OK);
    }

    @GetMapping(value = "/myCC")
    @ApiOperation("我的抄送")
    @Log("我的抄送")
    @AnonymousAccess
    public ResponseEntity<Object> myCC(Long taskId){
        return new ResponseEntity<>(processTaskService.getMyCC(taskId), HttpStatus.OK);
    }

    @PostMapping(value = "/revokeTask")
    @ApiOperation("撤销任务")
    @Log("撤销任务")
    @AnonymousAccess
    public ResponseEntity<Object> revokeTask(@RequestBody Map<String,Object> resource){
        processTaskService.revokeTask(resource);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/completeTask")
    @ApiOperation("完成任务")
    @Log("完成任务")
    @AnonymousAccess
    public ResponseEntity<Object> completeTask(@RequestBody Map<String, String> resource){
        processTaskService.completeTask(resource.get("taskId"));
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/returnTask")
    @ApiOperation("退回任务")
    @Log("退回任务")
    @AnonymousAccess
    public ResponseEntity<Object> returnTask(@RequestBody Map<String,Object> resource){
        processTaskService.refundTask(resource);
        return new ResponseEntity<>(HttpStatus.OK);
    }

}
